<?php


class User_model extends CI_Model{
	
	private function get_position($id){
		$this->db->where("position_id",$id);
		$query = $this->db->get("position");
		$rs = $query->result();
		return @$rs[0]->position_name;
	}

	private function get_department($id){
		$this->db->where("department_id",$id);
		$query = $this->db->get("department");
		$rs = $query->result();
		return @$rs[0]->department_name;
	}
	private function get_factory($id){
		$this->db->where("factory_id",$id);
		$query = $this->db->get("factory");
		$rs = $query->result();
		return @$rs[0]->factory_name;
	}
	
	
	public function getFullName($user_id){
		$this->db->where("user_id",$user_id);
		$query = $this->db->get("user");
		$rs = $query->result();
		return @$rs[0]->user_fname." ".@$rs[0]->user_lname;
		 
	}
	///////////////////////////////////////
	
	public function getAllPosition( ){
		//$this->db->where("position_id",$id);
		$query = $this->db->get("position");
		return $query->result();
		//return @$rs[0]->position_name;
	}

	public function getAllDepartment( ){
		//$this->db->where("position_id",$id);
		$query = $this->db->get("department");
		return $query->result();
		//return @$rs[0]->position_name;
	}
	
	public function getAllFactory( ){
		//$this->db->where("position_id",$id);
		$query = $this->db->get("factory");
		return $query->result();
		//return @$rs[0]->position_name;
	}
	
	///////////////////////////////////////////
	
	public function auten_session($rs){
		$data=$rs[0];
		$newdata = array(
				'user_id'  => $data->user_id,
				'admin'     => $data->user_admin,
				'username'  => $data->user_username,
				'email'     => $data->user_email,
				'fullname'  => $data->user_fname." ".$data->user_lname,
				'position_id'     => $data->position_id,
				'department_id'  => $data->department_id,
				'factory_id'     => $data->factory_id,
				'position'     => $this->get_position($data->position_id),
				'department'  => $this->get_department($data->department_id),
				'factory'     => $this->get_factory( $data->factory_id),

				'mobile'  => $data->user_mobile,
				'login_time'     => date("Y-m-d H:i:s"),
				'logged_in' => TRUE
		);
		
		$this->session->set_userdata($newdata);
		
	}
	
	public function delUserDB($user_id)
	{
		$this->db->where('user_id', $user_id);
		$this->db->delete('user'); 
		redirect('user/manageUser','refresh');
	}
	public function addUserDB($data){
		$this->db->insert("user",$data);
		return 1;
	}
	public function updateUserDB($input)
	{
		$this->db->where('user_id',$input['user_id']);
		$this->db->update('user',$input);
		redirect('user/manageUser','refresh');
	}
	public function allUserDB($user_id)
	{
			$this->db->select('*');
			$this->db->from('user');
			$this->db->join('position','position.position_id = user.position_id');
			$this->db->join('department','department.department_id = user.department_id');
			$this->db->join('factory','factory.factory_id = user.factory_id');
			$this->db->where('user_id',$user_id);
			$rs= $query = $this->db->get();
			return $data['rs'] = $rs->result_array();
	}
	public function allUser()
	{
			$this->db->select('*');
			$this->db->from('user');
			$this->db->join('position','position.position_id = user.position_id');
			$this->db->join('department','department.department_id = user.department_id');
			$this->db->join('factory','factory.factory_id = user.factory_id');
			$rs= $query = $this->db->get();
			return $data['rs'] = $rs->result_array();
	}

	
	public function getUser(){
		//$query = $this->db->get("user");
		//return json_encode($query->result());
		$this->db->select('*');
		$this->db->from('user');
		$this->db->join('position', 'position.position_id = user.position_id');
		$this->db->join('department', 'department.department_id = user.department_id');
		$this->db->join('factory', 'factory.factory_id = user.factory_id');
		$this->db->order_by("user_id", "asc");
		$query = $this->db->get();
		return json_encode($query->result());
		//echo "<pre>";
		//print_r($query->result());
	}
	
	
	public function checkLogin($inData){
		 $this->db->where("user_username",$inData["username"]);
		 $this->db->where("user_password",md5($inData["password"]));
		 $query = $this->db->get("user");
		 if($query->num_rows() > 0){
		 	$rs = $query->result();
		 	$this->auten_session($rs);
		 	return 1;
		 }
		 else{
		 	return 0;
		 }
	}
 
}
?>
